import os
import requests
from pathlib import Path

def download_font(url, filepath):
    print(f"Downloading {url} to {filepath}")
    response = requests.get(url)
    response.raise_for_status()
    
    # 确保目录存在
    os.makedirs(os.path.dirname(filepath), exist_ok=True)
    
    with open(filepath, 'wb') as f:
        f.write(response.content)
    print(f"Downloaded {filepath}")

def main():
    # Font Awesome 字体文件的基础 URL
    base_url = "https://use.fontawesome.com/releases/v5.15.4/webfonts"
    
    # 需要下载的字体文件
    fonts = [
        "fa-solid-900.woff2",
        "fa-solid-900.woff",
        "fa-solid-900.ttf",
        "fa-regular-400.woff2",
        "fa-regular-400.woff",
        "fa-regular-400.ttf"
    ]
    
    # 获取当前脚本所在目录
    base_dir = Path(__file__).parent
    webfonts_dir = base_dir / 'static' / 'webfonts'
    
    # 下载每个字体文件
    for font in fonts:
        url = f"{base_url}/{font}"
        filepath = webfonts_dir / font
        try:
            download_font(url, filepath)
        except Exception as e:
            print(f"Error downloading {font}: {str(e)}")

if __name__ == '__main__':
    main()